﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vds2465Telegrams;

namespace Vds2465Parser
{
    internal static class GenParse_ReportType_Device
    {
        #region private constants

        internal const byte MsgBlockOnlyValue = 0x50;

        private const byte GeneralByte = 0x00;
        private const byte DoorContactByte = 0x06;
        private const byte MessageBufferOverflowByte = 0x04;
        private const byte ResetByte = 0x02;
        private const byte RestartByte = 0x03;
        private const byte SwitchOffByte = 0x01;
        private const byte SystemDisruptionByte = 0x05;

        #endregion private constants

        #region internal methods

        internal static byte GenerateByte(ReportType_Device reportTypeDevice)
        {
            byte resultByte = 0x0;

            // Device has 0x1 in Bits 6,5,4
            resultByte += MsgBlockOnlyValue;

            // add the report type sub type
            if (reportTypeDevice.ReportSubType == eReportSubType_Device.General)
            {
                resultByte |= GeneralByte;
                return resultByte;
            }

            // add the report type sub type
            if (reportTypeDevice.ReportSubType == eReportSubType_Device.DoorContact)
            {
                resultByte |= DoorContactByte;
                return resultByte;
            }

            if (reportTypeDevice.ReportSubType == eReportSubType_Device.MessageBufferOverflow)
            {
                resultByte |= MessageBufferOverflowByte;
                return resultByte;
            }

            if (reportTypeDevice.ReportSubType == eReportSubType_Device.Reset)
            {
                resultByte |= ResetByte;
                return resultByte;
            }

            if (reportTypeDevice.ReportSubType == eReportSubType_Device.Restart)
            {
                resultByte |= RestartByte;
                return resultByte;
            }

            if (reportTypeDevice.ReportSubType == eReportSubType_Device.SwitchOff)
            {
                resultByte |= SwitchOffByte;
                return resultByte;
            }

            if (reportTypeDevice.ReportSubType == eReportSubType_Device.SystemDisruption)
            {
                resultByte |= SystemDisruptionByte;
                return resultByte;
            }

            // TODO handle error (unknown eReportSubType_Device)
            return resultByte;
        }

        internal static bool ParseByte(byte byteValue, out eReportSubType_Device reportSubTypeDevice)
        {
            // set arbitary default value
            reportSubTypeDevice = eReportSubType_Device.General;

            // get only bits 6,5,4
            byte messageBlock = (byte)(byteValue & (byte)0x70);
            if (messageBlock == MsgBlockOnlyValue)
            {
                // get only bits 3,2,1,0
                byte subType = (byte)(byteValue & (byte)0x0f);
                if (subType == GeneralByte)
                {
                    reportSubTypeDevice = eReportSubType_Device.General;
                    return true;
                }

                if (subType == DoorContactByte)
                {
                    reportSubTypeDevice = eReportSubType_Device.DoorContact;
                    return true;
                }

                if (subType == MessageBufferOverflowByte)
                {
                    reportSubTypeDevice = eReportSubType_Device.MessageBufferOverflow;
                    return true;
                }

                if (subType == ResetByte)
                {
                    reportSubTypeDevice = eReportSubType_Device.Reset;
                    return true;
                }

                if (subType == RestartByte)
                {
                    reportSubTypeDevice = eReportSubType_Device.Restart;
                    return true;
                }

                if (subType == SwitchOffByte)
                {
                    reportSubTypeDevice = eReportSubType_Device.SwitchOff;
                    return true;
                }
                if (subType == SystemDisruptionByte)
                {
                    reportSubTypeDevice = eReportSubType_Device.SystemDisruption;
                    return true;
                }

                // bits 3,2,1,0 are wrong
                return false;
            }

            // bits 6,5,4 are wrong
            return false;
        }

        #endregion internal methods
    }
}

